package server.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import server.db.DbAdapter;
import server.db.DbAdapterMySql;

public class MySqlDao implements AbstractDAO {
	DbAdapter adapter = new DbAdapterMySql();

	@Override
	public String insertNewUser(String name, String passwd) {
		Connection con = null;
		Statement st = null;
		PreparedStatement insert = null;

		if (checkNick(name)) {
			return "This name exist in DB";
		}

		String sql = "INSERT INTO `users` (`nick_name2` ,`passwd`,`is_online`,`ip`,`port`)VALUES (?, ?,true,NULL,NULL)";
		try {
			con = adapter.getConnection();
			insert = con.prepareStatement(sql);

			insert.setString(1, name);
			insert.setString(2, passwd);

			insert.execute();

		} catch (Exception e) {
			return e.getMessage();
		} finally {
			try {
				con.close();
				insert.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		return "Successful registration";
	}

	private boolean checkNick(String nick) {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;

		try {
			con = adapter.getConnection();

			st = con.createStatement();

			rs = st.executeQuery("SELECT `nick_name`" + "FROM `users`"
					+ "WHERE `nick_name` LIKE '" + nick + "'");
			return rs.next();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
				st.close();
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	@Override
	public boolean login(String name, String passwd, String ip) {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;

		boolean trueUser = false;

		try {
			con = adapter.getConnection();

			st = con.createStatement();

			rs = st.executeQuery("SELECT `nick_name`" + "FROM `users`"
					+ "WHERE `nick_name` LIKE '" + name
					+ "'AND `passwd` LIKE '" + passwd + "'");
			if (!rs.next()) {
				return false;
			} else {
				trueUser = true;
				return true;
			}
		} catch (Exception e) {
			return false;
		} finally {
			try {
				con.close();
				st.close();
				rs.close();
				String updateStatus = null;
				if (trueUser) {
					updateStatus = "UPDATE `users` SET `ip` = '"
							+ ip
							+ "',`is_online` = '1' WHERE `users`.`nick_name` like '"
							+ name + "'";
					System.out.println(updateStatus);
					updateDate(updateStatus);
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void updateDate(String sql) {
		Connection con = null;
		Statement st = null;

		try {
			con = adapter.getConnection();

			st = con.createStatement();

			st.executeUpdate(sql);

		} catch (Exception e) {
		} finally {
			try {
				con.close();
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
